package com.lzy.cloud.goods.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.dtos.CarCategoryDto;
import com.lzy.cloud.goods.facade.CarCategoryFacade;
import com.lzy.cloud.goods.request.CarCategoryRequest;
import com.lzy.cloud.goods.response.CarCategoryResponse;
import com.lzy.cloud.goods.service.CarCategoryService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by WIN7 on 2018/6/11.
 */
@RestController
@RequestMapping("/goods/car/category")
public class CarCategoryController {

    @Autowired
    CarCategoryService carCategoryService;

    @Autowired
    CarCategoryFacade carCategoryFacade;

    /**
     * 新增车型类别
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody(required = false) CarCategoryRequest request){
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = carCategoryService.add(DozerUtils.map(request, CarCategoryDto.class));
        Ensure.that(ok).isTrue("00030001");
        return new Result(ok);
    }

    /**
     * 删除车型类别
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del/{ids}",method = RequestMethod.GET)
    public Result del(@PathVariable("ids") String ids){
        Boolean ok = carCategoryFacade.del(ids);
        Ensure.that(ok).isTrue("00030002");
        return new Result();
    }

    /**
     * 编辑车型类别
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result update(@RequestBody CarCategoryRequest request){
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = carCategoryService.update(DozerUtils.map(request,CarCategoryDto.class));
        Ensure.that(ok).isTrue("00030003");
        return new Result();
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}" ,method = RequestMethod.GET)
    public Result detail(@PathVariable("id") Long id){
        CarCategoryDto carCategoryDto = carCategoryService.selectById(id);
        Ensure.that(carCategoryDto).isNotNull("00030004");
        return new Result(DozerUtils.map(carCategoryDto,CarCategoryResponse.class));
    }

    /**
     * 查询车型类别列表（分页）
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list" ,method = RequestMethod.GET)
    public Result selectList(CarCategoryRequest request){
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<CarCategoryDto> pageInfo = carCategoryService.selectList(pager,DozerUtils.map(request,CarCategoryDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), CarCategoryResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 启用/禁用
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/status",method = RequestMethod.POST)
    public Result status( CarCategoryRequest request){
        Ensure.that(request).isNotNull("00010001");
        CarCategoryDto dto = new CarCategoryDto();
        dto.setId(request.getId());
        dto.setStatus(request.getStatus());
        Boolean ok = carCategoryService.update(dto);
        Ensure.that(ok).isTrue("00030005");
        return new Result(ok);
    }
}
